package com.lecture.springlecture.event.java.order;

import java.util.ArrayList;
import java.util.List;

public class OrderService {
    private List<OrderEventListener> listeners = new ArrayList<>();

    public void addListener(OrderEventListener listener) {
        listeners.add(listener);
    }

    public void removeListener(OrderEventListener listener) {
        listeners.remove(listener);
    }

    public void placeOrder(String orderNumber) {
        System.out.println("Placing order: " + orderNumber);
        OrderEvent event = new OrderEvent(this, orderNumber);
        notifyListeners(event);
    }

    private void notifyListeners(OrderEvent event) {
        for (OrderEventListener listener : listeners) {
            listener.handleOrderEvent(event);
        }
    }
}